﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ModelDrawerTool : Bambom.UnitySingleton<ModelDrawerTool>
{
    private Dictionary<int, AbstractShapeDrawer> shapeMap = new Dictionary<int, AbstractShapeDrawer>();

    private List<GameObject> pool = new List<GameObject>();

    private Transform cacheRoot;
    private GameObject prefab;
    public override void Awake()
    {
        base.Awake();
        if (prefab == null)
        {
            prefab =Instantiate<GameObject>(Resources.Load<GameObject>("AbstractShapDrawer"));
            prefab.gameObject.SetActive(false);
        }
        if (cacheRoot == null)
        {
            GameObject obj = new GameObject("CacheShapDrawerRoot");
            cacheRoot = obj.transform;
        }
    }

    public void GizmosDrawerCube(BoxCollider collider)
    {
        Gizmos.DrawCube(collider.center, collider.size);
        Gizmos.DrawWireCube(collider.center, collider.size);
    }
    public void GizmosDrawerSphere(Vector3 center, Vector3 size)
    {

    }
    public T Spawn<T>() where T : AbstractShapeDrawer
    {
        GameObject temp = null;
        if (pool.Count > 0)
        {
            temp = pool[0];
            pool.RemoveAt(0);
        }
        else
        {
            temp = CreateDrawer();
        }
        var t = temp.AddComponent<T>();
        var guid = temp.GetInstanceID();
        t.GUID = guid;
        shapeMap[guid] = t;
        temp.gameObject.SetActive(true);
        return t;
    }

    private GameObject CreateDrawer()
    {
        var newObj = Instantiate<GameObject>(prefab);
        return newObj;
     }
    public void Recycle(AbstractShapeDrawer obj)
    {
        if (shapeMap.ContainsKey(obj.GUID))
        {
            shapeMap.Remove(obj.GUID);
            obj.Reset();
            obj.transform.SetParent(cacheRoot);
            Destroy(obj);
        }
    }
}
